package cn.bud.msgbus.provider.client;

import cn.bud.msgbus.provider.client.provider.ProviderProxy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommandLineIoc implements InitializingBean, BeanFactoryAware {

    private ConfigurableListableBeanFactory beanFactory;

    @Resource
    private BusProviderScanner providerScanner;




	//注入beanFactory
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = (beanFactory instanceof ConfigurableListableBeanFactory)
                ? (ConfigurableListableBeanFactory) beanFactory : null;
    }




	//初始化时将自定义的bean放入spring容器中使用
    @Override
    public void afterPropertiesSet() throws Exception {
        List<ProviderProxy> providerProxies = providerScanner.scanProviders("com.zl9c.**");
        for (ProviderProxy providerProxy : providerProxies) {
            beanFactory.registerSingleton(providerProxy.getBusBeanName(), providerProxy.getProxyObj());
        }
    }
}
